//
//  UIButton+LQExtension.m
//  LQProductIntelligenceService
//
//  Created by Yim on 16/7/1.
//  Copyright © 2016年 张宁. All rights reserved.
//

#import "UIButton+LQExtension.h"

@implementation UIButton (LQExtension)
/**
 * 设置倒角为5的按钮
 */
- (void) setLineFrameLayer
{
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = 5;
    self.layer.borderWidth = 1;
    self.layer.borderColor = [UIColor grayColor].CGColor;
}

- (void) setBtnImageRight
{
    CGFloat imageViewWidth = CGRectGetWidth(self.imageView.frame);
    CGFloat labelWidth = CGRectGetWidth(self.titleLabel.frame);
    [self setImageEdgeInsets:UIEdgeInsetsMake(0, 0 + labelWidth + imageViewWidth, 0, 0 - (labelWidth + imageViewWidth))];
    [self setTitleEdgeInsets:UIEdgeInsetsMake(0, 0 - imageViewWidth, 0, 0 + imageViewWidth)];
}


// 返回一个文本button
+ (instancetype) buttonWithTitle:(NSString *)title withHighTitle:(NSString *)highTitle addTarget:(id)target addAction:(SEL)action
{
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.bounds = (CGRect){CGPointZero,[button backgroundImageForState:UIControlStateNormal].size};
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [button setTitle:highTitle forState:UIControlStateHighlighted];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    button.contentHorizontalAlignment = UIControlContentVerticalAlignmentCenter;
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}
/**
 *  * 返回一个带上下箭头高亮文本的button
 */
+ (instancetype) buttonWithTitleAndImage:(NSString *)title withHightTitle:(NSString *)highTitle addTarget:(id)target addAction:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.bounds = (CGRect){CGPointZero,[button backgroundImageForState:UIControlStateNormal].size};
    [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    [button setImage:[UIImage imageNamed:@"arrow_down_grey"] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [button setTitle:highTitle forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [button setImage:[UIImage imageNamed:@"arrow_right_grey"] forState:UIControlStateHighlighted];
    
        CGFloat imageViewWidth = CGRectGetWidth(button.imageView.frame);
        CGFloat labelWidth = CGRectGetWidth(button.titleLabel.frame);
        [button setImageEdgeInsets:UIEdgeInsetsMake(0, 0 + labelWidth + imageViewWidth, 0, 0 - (labelWidth + imageViewWidth))];
        [button setTitleEdgeInsets:UIEdgeInsetsMake(0, 0 - imageViewWidth, 0, 0 + imageViewWidth)];
    
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
    
}
// 返回一个文本barButton
+ (instancetype) buttonWithTitle:(NSString *)title withSelectedTitle:(NSString *)SelectedTitle addTarget:(id)target addAction:(SEL)action
{
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
//    button.bounds = (CGRect){CGPointZero,[button backgroundImageForState:UIControlStateNormal].size};
    button.bounds = CGRectMake(0, 0, 60, 30);
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    
    [button setTitle:SelectedTitle forState:UIControlStateSelected];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    button.titleLabel.font = [UIFont systemFontOfSize:15];
    button.contentHorizontalAlignment = UIControlContentVerticalAlignmentCenter;
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}

/**
 * 返回一个带上下箭头图片点击文本的button
 */
+ (instancetype) buttonWithAndImageTitle:(NSString *)title withSelectedTitle:(NSString *)SelectedTitle addTarget:(id)target addAction:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.bounds = (CGRect){CGPointZero,[button backgroundImageForState:UIControlStateNormal].size};
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    [button setTitle:title forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"arrow_down_grey"] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [button setTitle:SelectedTitle forState:UIControlStateSelected];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    [button setImage:[UIImage imageNamed:@"arrow_right_grey"] forState:UIControlStateSelected];

//    CGFloat imageViewWidth = CGRectGetWidth(button.imageView.frame);
//    CGFloat labelWidth = CGRectGetWidth(button.titleLabel.frame);
//    [button setImageEdgeInsets:UIEdgeInsetsMake(0, 0 + labelWidth + imageViewWidth, 0, 0 - (labelWidth + imageViewWidth))];
//    [button setTitleEdgeInsets:UIEdgeInsetsMake(0, 0 - imageViewWidth, 0, 0 + imageViewWidth)];

    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}

/**
 *  返回一个带自定义图片文本的button
 */
+ (instancetype) buttonWithTitle:(NSString *)title withImage:(NSString *)image withSelectedTitle:(NSString *)SelectedTitle withSelectedImage:(NSString *)selectedImage addTarget:(id)target addAction:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.bounds = (CGRect){CGPointZero,[button backgroundImageForState:UIControlStateNormal].size};
    [button setTitle:title forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:image] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [button setTitle:SelectedTitle forState:UIControlStateSelected];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    [button setImage:[UIImage imageNamed:selectedImage] forState:UIControlStateSelected];
    button.titleLabel.font = [UIFont systemFontOfSize:15];
    
    CGFloat btn_centerX = CGRectGetMidX(button.bounds);
    CGFloat label_centerX = CGRectGetMidX(button.titleLabel.frame);
    CGFloat image_centerX = CGRectGetMidX(button.imageView.frame);
    [button setImageEdgeInsets:UIEdgeInsetsMake(0, 0 + (btn_centerX - image_centerX), 0, 0 + (btn_centerX - image_centerX))];
    [button setTitleEdgeInsets:UIEdgeInsetsMake(0, 0 - (label_centerX - btn_centerX), 0, 0 + (label_centerX - btn_centerX))];
    
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}

/**
 *  返回一个返回btn
 */
+ (instancetype) buttonWithBackAddTarget:(id)target addAction:(SEL)action
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, 20, 60, 30);
    button.contentEdgeInsets = UIEdgeInsetsMake(0, -5, 0, 0);
    button.titleLabel.font = [UIFont systemFontOfSize:15];
    [button setImage:[UIImage imageNamed:@"navigationButtonReturn"] forState:UIControlStateNormal];
    [button setImage:[UIImage imageNamed:@"navigationButtonReturnClick"] forState:UIControlStateHighlighted];
    [button setTitle:@"返回" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    return button;
}

@end
